<!DOCTYPE html>
<html>

<head>
<title>Password Generator</title>
</head>

<body onload="generate()">


Length:
<select id="length">
<option value="8">8</option>
<option value="10">10</option>
<option value="12" selected>12</option>
<option value="16">16</option>
</select>

<input type="checkbox" id="specChars">Use special characters<br>
<p id="value"></p>

<button onclick="generate()">Generate</button>

<script>
function generate()
{
var randNums = new Array();
var opts = 3;
var specialChars = new Array("-", "+", "_", "?", "!", "%", "^", "&", "*");

var useSpecChars = document.getElementById("specChars").checked;
if(useSpecChars)
{
    opts = 4;
}

// dummy loop
var loopCnt = Math.floor((Math.random() * 103) + 1);
for (var i = 0; i < loopCnt; i++)
{
    var rnd = Math.random();
}

var lenVal = document.getElementById("length")
var len = parseInt(lenVal.options[lenVal.selectedIndex].value, 10);
for (var i = 0; i < len; i++)
{
    var t = Math.floor((Math.random() * opts) + 1);
    var v = 0;
    if (t == 1)
        v = Math.floor((Math.random() * 25) + 65);  // Upper-case
    else if (t == 2)
        v = Math.floor((Math.random() * 25) + 97);  // Lower-case
    else if (t == 3)
        v = Math.floor((Math.random() * 10) + 48);  // Numbers
    else    // Special characters
    {
        var idx = Math.floor(Math.random() * specialChars.length);
        //alart("char = ");// + specialChars[idx]);
        v = specialChars[idx].charCodeAt(0);
    }

    randNums[i] = v;
}
var x = document.getElementById("value");
x.innerHTML = numsToString(randNums);
document.getElementById("value").select();
}

function numsToString(nums)
{
    var chars = "";
    for (var i = 0; i < nums.length; i++)
    {
        chars += String.fromCharCode(nums[i]);
    }
    return chars;

}
</script>

</body>
</html>

